package defectsim;

public class Stress {

	public enum ExternalStressType {
		Cyclic,
		Tensile
	}
	
	private double currentExternalStress;
	private double currentFrictionStress;
	private double maxFrictionStress;
	private double maxExternalStress;
	private ExternalStressType stressType;
	private double cyclicTickCount;
	
		
	public double getCurrentExternalStress(double timeTick) {
		switch (stressType) {
		case Cyclic:
			currentExternalStress = CalculateCylicStress(timeTick);
			break;
		case Tensile:
			currentExternalStress = maxExternalStress;
			break;
			
		}
		return currentExternalStress;
	}

	public double getCurrentFrictionStress(double timeTick) {
		currentFrictionStress = calculateFrictionStress(timeTick);
		return currentFrictionStress;
	}

	public double getMaxFrictionStress() {
		return maxFrictionStress;
	}

	public void setMaxFrictionStress(double globalFrictionStress) {
		this.maxFrictionStress = globalFrictionStress;
	}

	public double getMaxExternalStress() {
		return maxExternalStress;
	}

	public void setMaxExternalStress(double globalExternalStress) {
		this.maxExternalStress = globalExternalStress;
	}

	public ExternalStressType getStressType() {
		return stressType;
	}

	public void setStressType(ExternalStressType stressType) {
		this.stressType = stressType;
	}

	public Stress(double gExStress, double gFricStress, ExternalStressType type, double tickCount ) {
		maxExternalStress = gExStress;
		maxFrictionStress = gFricStress;
		stressType = type;
		cyclicTickCount = tickCount;
	}
	
	private double calculateFrictionStress(double timeTick){
		// friction stress does not depend on time for now
		return maxFrictionStress;
	}

	private double CalculateCylicStress(double timeTick){
		double phaseAngle = 0.0;
		double timeStep = 1e-1;
		double frequency = 1 / (cyclicTickCount * timeStep);
		double angularFrequency = 2 * Math.PI * frequency;
		
		double cyclicStress = maxExternalStress * 
		Math.sin(angularFrequency * timeTick * timeStep + phaseAngle);

		return cyclicStress;
	}
}
